package com.zuoban.shiro.chain;

import com.zuoban.util.JwtUtil;
import com.zuoban.util.RedisUtil;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigInteger;

/**
 * token解析成功，并且没过期 来到这个过滤链
 */
@Component
@Order(3)
public class RedisTokenChain implements AuthChain{

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private JwtUtil jwtUtil;

    @Override
    public BigInteger authentication(String token) {
        String userId = jwtUtil.getClaims(token).getClaims().get("userId").asString();
        String redisToken = (String) redisUtil.get("token|"+userId);
        if (StringUtils.isEmpty(redisToken)){
            throw new RuntimeException("token已过期，请重新登录");
        }
        return new BigInteger(userId);
    }
}
